package com.zekai.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.common.dao.BaseDao;
import com.common.entity.DropOutOfSchool;
import com.common.entity.Employee;
import com.zekai.dao.IDropOutOfSchoolDao;

import utils.Page;


public class DropOutOfSchoolDaoImpl extends BaseDao<DropOutOfSchool> implements IDropOutOfSchoolDao{

//	public static void main(String[] args) {
//		IDropOutOfSchoolDao dao=new DropOutOfSchoolDaoImpl();
//		Page<DropOutOfSchool> dPage=dao.getPage("", 1, 2);
//		List<DropOutOfSchool> dropOutOfSchools=dPage.getList();
//		System.out.println(dropOutOfSchools);
//	}
	@Override
	public int getCount(String condition) {
		int count=0;
		PreparedStatement ps=null;
		ResultSet resultSet=null;
		try {
			String sql="SELECT COUNT(1) FROM dropout WHERE stuNumber LIKE ? OR leaveReson LIKE ?  ";	
			ps=this.getConnection().prepareStatement(sql);
			ps.setObject(1, "%"+condition+"%");
			ps.setObject(2, "%"+condition+"%");
			resultSet=ps.executeQuery();
			if (resultSet.next()) {
				count=resultSet.getInt(1);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return count;
	}

	@Override
	public List<DropOutOfSchool> getOnePageInfo(String condition, int currentPage, int pageSize) {
		List<DropOutOfSchool> dropOutOfSchools=new ArrayList<DropOutOfSchool>();
		PreparedStatement ps=null;
		ResultSet resultSet=null;
		String pageSql="SELECT id,stuNumber,leaveDate,magNumber,leaveReson,reply,isAgree FROM dropout WHERE stuNumber LIKE ? OR leaveReson LIKE ? limit ?,? ";
		try {
			ps=this.getConnection().prepareStatement(pageSql);
			ps.setObject(1, "%"+condition+"%");
			ps.setObject(2, "%"+condition+"%");
			ps.setObject(3, (currentPage-1)*pageSize);
			ps.setObject(4, pageSize);
			resultSet=ps.executeQuery();
			while(resultSet.next()) {
				DropOutOfSchool dropOutOfSchool=this.getEntity(resultSet);
				dropOutOfSchools.add(dropOutOfSchool);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		return dropOutOfSchools;
	}

	@Override
	public Page<DropOutOfSchool> getPage(String condition, int currentPage, int pageSize) {
		int count=this.getCount(condition);
		List<DropOutOfSchool> list=this.getOnePageInfo(condition, currentPage, pageSize);
		return new Page<DropOutOfSchool>(currentPage, count, pageSize, list);
	}

	@Override
	public int save(DropOutOfSchool dropOutOfSchool) {
		String sql="INSERT INTO `dropout`(id,stuNumber,leaveDate,magNumber,leaveReson,reply,isagree)VALUES(?,?,?,?,?,?,?)";
		List<Object> dropOutOfSchools=new ArrayList<Object>();
		dropOutOfSchools.add(dropOutOfSchool.getDropId());
		dropOutOfSchools.add(dropOutOfSchool.getStuNumber());
		dropOutOfSchools.add(dropOutOfSchool.getDropLeaveDate());
		dropOutOfSchools.add(dropOutOfSchool.getMagNumber());
		dropOutOfSchools.add(dropOutOfSchool.getDropLeaveReson());
		dropOutOfSchools.add(dropOutOfSchool.getDropReply());
		dropOutOfSchools.add(dropOutOfSchool.getDropIsagree());
		return this.excuteUpdate(sql, dropOutOfSchools.toArray());
	}

	@Override
	public int deleteById(int id) {
		String sql=" delete from `dropout` where id=? ";
		return this.excuteUpdate(sql, new Object[]{id});
	}

	@Override
	public int update(DropOutOfSchool dropOutOfSchool) {
		String sql="UPDATE `dropout` SET stuNumber=? ,leaveDate=? ,magNumber=? ,leaveReson=? ,reply=?,isagree=? WHERE id=? ";
		List<Object> dropOutOfSchools=new ArrayList<Object>();
		dropOutOfSchools.add(dropOutOfSchool.getStuNumber());
		dropOutOfSchools.add(dropOutOfSchool.getDropLeaveDate());
		dropOutOfSchools.add(dropOutOfSchool.getMagNumber());
		dropOutOfSchools.add(dropOutOfSchool.getDropLeaveReson());
		dropOutOfSchools.add(dropOutOfSchool.getDropReply());
		dropOutOfSchools.add(dropOutOfSchool.getDropIsagree());
		dropOutOfSchools.add(dropOutOfSchool.getDropId());
		return this.excuteUpdate(sql, dropOutOfSchools.toArray());
	}

	@Override
	public DropOutOfSchool getDropOutOfSchoolById(int id) {
		DropOutOfSchool dropOutOfSchool=new DropOutOfSchool();
		String sql="SELECT stuNumber,leaveDate,magNumber,leaveReson,reply,isagree FROM dropout WHERE id=? ";
		try {
			PreparedStatement ps=this.getConnection().prepareStatement(sql);
			ps.setObject(1, id);
			ResultSet resultSet=ps.executeQuery();
			while(resultSet.next()) {
				dropOutOfSchool=this.getEntity(resultSet);	}		
		} catch (Exception e) {
			// TODO: handle exception
		}
		return dropOutOfSchool;
	}

	@Override
	public int getId(String code, String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public DropOutOfSchool getEntity(ResultSet rs) {
		DropOutOfSchool dropOutOfSchool=new DropOutOfSchool();
		try {
			dropOutOfSchool.setDropId(rs.getInt("id"));
			dropOutOfSchool.setStuNumber(rs.getInt("stuNumber"));
			dropOutOfSchool.setDropLeaveDate(rs.getDate("leaveDate"));
			dropOutOfSchool.setDropLeaveReson(rs.getString("leaveReson"));
			dropOutOfSchool.setDropReply(rs.getString("reply"));
			dropOutOfSchool.setMagNumber(rs.getInt("magNumber"));
			dropOutOfSchool.setDropIsagree(rs.getInt("isAgree"));	
		} catch (Exception e) {
		}

		return dropOutOfSchool;
	}

	@Override
	public boolean deleteDropOutOfSchoolByIds(int[] ids) {
		boolean isYes=false;
		try {
			for (int i = 0; i < ids.length; i++) {
				this.deleteById(ids[i]);
			}
			isYes=true;
		} catch (Exception e) {
			e.printStackTrace();
			isYes=false;
		}

		return isYes;
		
	}

	
}
